| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251 |
1×
6×
6×
5×
5×
1×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
5×
5×
5×
6×
6×
6×
15×
15×
15×
15×
15×
9×
9×
6×
6×
5×
6×
5×
5×
3×
3×
3×
3×
3×
2×
2×
9×
6×
6×
6×
6×
6×
5×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
6×
5×
1×
15×
15×
15×
9×
6×
6×
6×
1×
30×
1×
15×
6×
6×
5×
6×
7×
7×
35×
8×
8×
8×
8×
7×
7×
6×
6×
6×
6×
7×
| 'use strict';
angular.module('mgcrea.ngStrap.affix', ['mgcrea.ngStrap.helpers.dimensions', 'mgcrea.ngStrap.helpers.debounce'])
.provider('$affix', function () {
var defaults = this.defaults = {
offsetTop: 'auto',
inlineStyles: true
};
this.$get = function ($window, debounce, dimensions) {
var bodyEl = angular.element($window.document.body);
var windowEl = angular.element($window);
function AffixFactory(element, config) {
var $affix = {};
// Common vars
var options = angular.extend({}, defaults, config);
var targetEl = options.target;
// Initial private vars
var reset = 'affix affix-top affix-bottom';
var setWidth = false;
var initialAffixTop = 0;
var initialOffsetTop = 0;
var offsetTop = 0;
var offsetBottom = 0;
var affixed = null;
var unpin = null;
var parent = element.parent();
// Options: custom parent
Iif (options.offsetParent) {
if (options.offsetParent.match(/^\d+$/)) {
for (var i = 0; i < (options.offsetParent * 1) - 1; i++) {
parent = parent.parent();
}
} else {
parent = angular.element(options.offsetParent);
}
}
$affix.init = function () {
this.$parseOffsets();
initialOffsetTop = dimensions.offset(element[0]).top + initialAffixTop;
setWidth = !element[0].style.width;
// Bind events
targetEl.on('scroll', this.checkPosition);
targetEl.on('click', this.checkPositionWithEventLoop);
windowEl.on('resize', this.$debouncedOnResize);
// Both of these checkPosition() calls are necessary for the case where
// the user hits refresh after scrolling to the bottom of the page.
this.checkPosition();
this.checkPositionWithEventLoop();
};
$affix.destroy = function () {
// Unbind events
targetEl.off('scroll', this.checkPosition);
targetEl.off('click', this.checkPositionWithEventLoop);
windowEl.off('resize', this.$debouncedOnResize);
};
$affix.checkPositionWithEventLoop = function () {
// IE 9 throws an error if we use 'this' instead of '$affix'
// in this setTimeout call
setTimeout($affix.checkPosition, 1);
};
$affix.checkPosition = function () {
// if (!this.$element.is(':visible')) return
var scrollTop = getScrollTop();
var position = dimensions.offset(element[0]);
var elementHeight = dimensions.height(element[0]);
// Get required affix class according to position
var affix = getRequiredAffixClass(unpin, position, elementHeight);
// Did affix status changed this last check?
if (affixed === affix) return;
affixed = affix;
if (affix === 'top') {
unpin = null;
if (setWidth) {
element.css('width', '');
}
if (options.inlineStyles) {
element.css('position', (options.offsetParent) ? '' : 'relative');
element.css('top', '');
}
} else Iif (affix === 'bottom') {
if (options.offsetUnpin) {
unpin = -(options.offsetUnpin * 1);
} else {
// Calculate unpin threshold when affixed to bottom.
// Hopefully the browser scrolls pixel by pixel.
unpin = position.top - scrollTop;
}
if (setWidth) {
element.css('width', '');
}
if (options.inlineStyles) {
element.css('position', (options.offsetParent) ? '' : 'relative');
element.css('top', (options.offsetParent) ? '' : ((bodyEl[0].offsetHeight - offsetBottom - elementHeight - initialOffsetTop) + 'px'));
}
} else { // affix === 'middle'
unpin = null;
Eif (setWidth) {
element.css('width', element[0].offsetWidth + 'px');
}
if (options.inlineStyles) {
element.css('position', 'fixed');
element.css('top', initialAffixTop + 'px');
}
}
// Add proper affix class
element.removeClass(reset).addClass('affix' + ((affix !== 'middle') ? '-' + affix : ''));
};
$affix.$onResize = function () {
$affix.$parseOffsets();
$affix.checkPosition();
};
$affix.$debouncedOnResize = debounce($affix.$onResize, 50);
$affix.$parseOffsets = function () {
var initialPosition = element.css('position');
// Reset position to calculate correct offsetTop
if (options.inlineStyles) {
element.css('position', (options.offsetParent) ? '' : 'relative');
}
Eif (options.offsetTop) {
Eif (options.offsetTop === 'auto') {
options.offsetTop = '+0';
}
Eif (options.offsetTop.match(/^[-+]\d+$/)) {
initialAffixTop = - options.offsetTop * 1;
Iif (options.offsetParent) {
offsetTop = dimensions.offset(parent[0]).top + (options.offsetTop * 1);
} else {
offsetTop = dimensions.offset(element[0]).top - dimensions.css(element[0], 'marginTop', true) + (options.offsetTop * 1);
}
} else {
offsetTop = options.offsetTop * 1;
}
}
Eif (options.offsetBottom) {
Iif (options.offsetParent && options.offsetBottom.match(/^[-+]\d+$/)) {
// add 1 pixel due to rounding problems...
offsetBottom = getScrollHeight() - (dimensions.offset(parent[0]).top + dimensions.height(parent[0])) + (options.offsetBottom * 1) + 1;
} else {
offsetBottom = options.offsetBottom * 1;
}
}
// Bring back the element's position after calculations
if (options.inlineStyles) {
element.css('position', initialPosition);
}
};
// Private methods
function getRequiredAffixClass(_unpin, position, elementHeight) {
var scrollTop = getScrollTop();
var scrollHeight = getScrollHeight();
if (scrollTop <= offsetTop) {
return 'top';
} else Iif (_unpin !== null && (scrollTop + _unpin <= position.top)) {
return 'middle';
} else Iif (offsetBottom !== null && (position.top + elementHeight + initialAffixTop >= scrollHeight - offsetBottom)) {
return 'bottom';
}
return 'middle';
}
function getScrollTop() {
return targetEl[0] === $window ? $window.pageYOffset : targetEl[0].scrollTop;
}
function getScrollHeight() {
return targetEl[0] === $window ? $window.document.body.scrollHeight : targetEl[0].scrollHeight;
}
$affix.init();
return $affix;
}
return AffixFactory;
};
})
.directive('bsAffix', function ($affix, $window) {
return {
restrict: 'EAC',
require: '^?bsAffixTarget',
link: function postLink(scope, element, attr, affixTarget) {
var options = {scope: scope, target: affixTarget ? affixTarget.$element : angular.element($window)};
angular.forEach(['offsetTop', 'offsetBottom', 'offsetParent', 'offsetUnpin', 'inlineStyles'], function (key) {
if (angular.isDefined(attr[key])) {
var option = attr[key];
Iif (/true/i.test(option)) option = true;
if (/false/i.test(option)) option = false;
options[key] = option;
}
});
var affix = $affix(element, options);
scope.$on('$destroy', function () {
Eif (affix) affix.destroy();
options = null;
affix = null;
});
}
};
})
.directive('bsAffixTarget', function () {
return {
controller: function ($element) {
this.$element = $element;
}
};
});
|